Passed
Push — master ( 3278dc...27a5bd )
by Kevin Van
03:53 queued 11s
created

GamePage.renderScore   A

Complexity

Conditions 2

Size

Total Lines 9
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 9
rs 9.95
c 0
b 0
f 0
cc 2
1
import React, { Component, Fragment } from "react"
2
import LazyLoad from "react-lazy-load"
3
4
import { graphql } from "gatsby"
5
6
import moment from "moment"
7
import "moment/locale/nl-be"
8
9
import { mapPsdStatus } from "../scripts/helper"
10
11
import Layout from "../layouts/index"
12
import SEO from "../components/seo"
13
import Icon from "../components/Icon"
14
import MiniRanking from "../components/MiniRanking"
15
import Spinner from "../components/Spinner"
16
17
import iconBench from "../images/i_bench_2.png"
18
import iconCardRed from "../images/i_card_red.png"
19
import iconCardYellowRed from "../images/i_card_yellow_red.png"
20
import iconCardYellow from "../images/i_card_yellow.png"
21
import iconGoal from "../images/i_goal.png"
22
import iconStart from "../images/i_start.png"
23
import iconSubIn from "../images/i_sub_in.png"
24
import iconSubOut from "../images/i_sub_out.png"
25
26
class GamePage extends Component {
27
  constructor(props) {
28
    super(props)
29
30
    this.state = {
31
      data: [],
32
      loading: true,
33
    }
34
35
    const {
36
      data: {
37
        site: {
38
          siteMetadata: { kcvvPsdApi },
39
        },
40
      },
41
    } = this.props
42
43
    this.kcvvPsdApi = kcvvPsdApi
44
    this.matchId = this.props.id || null
45
  }
46
47
  updateData() {
48
    if (this.matchId === null) {
49
      return
50
    }
51
52
    const apiUrl = `${this.kcvvPsdApi}/match/${this.matchId}`
53
54
    fetch(apiUrl)
55
      .then((response) => response.json())
56
      .then((json) => this.setState({ data: json, loading: false }))
57
  }
58
59
  componentDidMount() {
60
    this.updateData()
61
  }
62
63
  render() {
64
    if (this.matchId === null) {
65
      return (
66
        <Layout>
67
          <section className="grid-container site-content">Geen match beschikbaar...</section>
68
        </Layout>
69
      )
70
    }
71
72
    moment.locale(`nl-be`)
73
74
    if (this.state.loading === false && this.state.data) {
75
      const { general = {}, substitutes = {}, lineup = {}, events = [] } = this.state.data
76
      const homeTeamId = general.homeClub?.id
77
      const ogImage = {
78
        src: general?.homeClub?.logo,
79
        width: 180,
80
        height: 180,
81
      }
82
83
      const { home: homeLineup = [], away: awayLineup = [] } = lineup || {}
84
      const { home: homeSubs = [], away: awaySubs = [] } = substitutes || {}
85
86
      const matchTime = moment(general.date)
87
88
      return (
89
        <Layout>
90
          <SEO
91
            lang="nl-BE"
92
            title={`Matchverslag ${general?.homeClub?.name} - ${general?.awayClub?.name}`}
93
            description={`Matchverslag ${general?.homeClub?.name} - ${general?.awayClub?.name}`}
94
            path={`/game/${general?.id}`}
95
            image={ogImage}
96
          />
97
98
          <section className="grid-container game-stats">
99
            <div className="grid-x grid-margin-x">
100
              <div className={`cell large-12 center game__details`}>
101
                <div className="game__teams">
102
                  <div className={`game__teams-inner`}>
103
                    <LazyLoad debounce={false}>
104
                      <img src={general.homeClub?.logo} alt={general.homeClub?.name} title={general.homeClub?.name} />
105
                    </LazyLoad>
106
                  </div>
107
                  {this.renderScore(general.goalsHomeTeam, general.goalsAwayTeam)}
108
                  <div className={`game__teams-inner`}>
109
                    <LazyLoad debounce={false}>
110
                      <img src={general.awayClub?.logo} alt={general.awayClub?.name} title={general.awayClub?.name} />
111
                    </LazyLoad>
112
                  </div>
113
                </div>
114
                <h1>{`${general.homeClub?.name} - ${general.awayClub?.name}`}</h1>
115
                <h4>{general.competitionType}</h4>
116
                <time dateTime={matchTime.format(`YYYY-MM-DD - H:mm`)}>
117
                  {matchTime.format(`dddd DD MMMM YYYY - H:mm`)}
118
                </time>
119
                <br />
120
121
                {general.status !== 0 && (
122
                  <span className={`game__status game__status--${general.status}`}>{mapPsdStatus(general.status)}</span>
123
                )}
124
125
                <br />
126
              </div>
127
              {(homeLineup.length !== 0 || awayLineup.length !== 0) && (
128
                <div className={`lineup__wrapper grid-x grid-margin-x cell large-12`}>
129
                  <div className={`cell large-6 lineup__wrapper--home`}>
130
                    <h3>{general.homeClub?.name}</h3>
131
                    {homeLineup && this.renderLineup(homeLineup, homeSubs)}
132
                  </div>
133
                  <div className={`cell large-6 lineup__wrapper--away`}>
134
                    <h3>{general.awayClub?.name}</h3>
135
                    {awayLineup && this.renderLineup(awayLineup, awaySubs)}
136
                  </div>
137
                </div>
138
              )}
139
140
              {events.length !== 0 && (
141
                <div className={`cell large-12 event__wrapper`}>
142
                  <h3>Gebeurtenissen</h3>
143
                  {events && this.renderEvents(events, homeTeamId)}
144
                </div>
145
              )}
146
            </div>
147
          </section>
148
149
          <MiniRanking
150
            teamId={general.homeTeamId || general.awayTeamId}
151
            homeTeam={general.homeClub?.name}
152
            awayTeam={general.awayClub?.name}
153
          />
154
        </Layout>
155
      )
156
    } else {
157
      return (
158
        <Layout>
159
          <section className="grid-container site-content">
160
            <Spinner />
161
          </section>
162
        </Layout>
163
      )
164
    }
165
  }
166
167
  renderScore = (resultHome, resultAway) => {
168
    return resultHome !== null && resultAway !== null ? (
169
      <div className={`match-details__vs match-details__vs--score`}>
170
        {this.renderScoreWithWinnerIndicator(resultHome, resultAway, `home`)}
171
        <span className={`match-details__divider`}>&nbsp;-&nbsp;</span>
172
        {this.renderScoreWithWinnerIndicator(resultAway, resultHome, `away`)}
173
      </div>
174
    ) : (
175
      <div className={`match-details__vs`}>VS</div>
176
    )
177
  }
178
179
  renderScoreWithWinnerIndicator = (result1, result2, extraClass) => {
180
    return result1 > result2 ? (
181
      <span className={`match-details__winner match-details__winner--${extraClass}`}>{result1}</span>
182
    ) : (
183
      <span className={`match-details__loser`}>{result1}</span>
184
    )
185
  }
186
187
  renderEvents(events, homeTeamId) {
188
    return <Fragment>{events.map((element, i) => this.renderEventLine(i, element, homeTeamId))}</Fragment>
189
  }
190
191
  renderEventLine(i, element, homeTeamId) {
192
    const homeTeam = element.clubId == homeTeamId
193
    let actionIcon = null
194
    let actionMessage = ``
195
    let actionText = ``
196
197
    switch (element.action) {
198
      case `geel`:
199
        actionIcon = iconCardYellow
200
        actionText = `Gele kaart voor`
201
        actionMessage = `Gele kaart`
202
        break
203
      case `rood`:
204
        actionIcon = iconCardRed
205
        actionText = `Rode kaart voor`
206
        actionMessage = `Rode kaart`
207
        break
208
      case `tweedegeel`:
209
        actionIcon = iconCardYellowRed
210
        actionText = `Tweede gele kaart voor`
211
        actionMessage = `Tweede gele kaart`
212
        break
213
      case `doelpunt`:
214
        actionIcon = iconGoal
215
        actionText = `${element?.goalsHome} - ${element?.goalsAway} — Doelpunt gescoord door`
216
        actionMessage = `Doelpunt`
217
        break
218
    }
219
220
    return (
221
      <div className={`event__row ${homeTeam ? `event__row--home` : `event__row--away`} grid-x grid-margin-x`} key={i}>
222
        {homeTeam && (
223
          <span className={`event__row__item event__row__item--home lineup__item--name cell small-10 large-4`}>
224
            {actionText} {element.playerName}
225
          </span>
226
        )}
227
        {homeTeam && (
228
          <span
229
            className={`event__row__item event__row__item--home lineup__item--action cell small-1 center`}
230
            style={{ backgroundImage: `url(${actionIcon})` }}
231
            title={actionMessage}
232
          ></span>
233
        )}
234
        <span className={`event__row__item lineup__item--time cell small-1 large-2 center`}>{element.minute}'</span>
235
        {homeTeam || (
236
          <span
237
            className={`event__row__item event__row__item--away lineup__item--action cell small-1 center`}
238
            style={{ backgroundImage: `url(${actionIcon})` }}
239
            title={actionMessage}
240
          ></span>
241
        )}
242
        {homeTeam || (
243
          <span className={`event__row__item event__row__item--away lineup__item--name cell small-10 large-4`}>
244
            {actionText} {element.playerName}
245
          </span>
246
        )}
247
      </div>
248
    )
249
  }
250
  renderLineup(lineup, substitutes) {
251
    return (
252
      <Fragment>
253
        {this.renderLineupHeader()}
254
        {lineup.map((element, i) => this.renderLineupLine(i, element))}
255
        <hr />
256
        {substitutes.map((element, i) => this.renderSubLine(i, element))}
257
      </Fragment>
258
    )
259
  }
260
261
  renderLineupHeader() {
262
    return (
263
      <div className={`lineup__header grid-x grid-margin-x`}>
264
        <span className={`lineup__header__item lineup__item--status cell small-1`}></span>
265
        <span className={`lineup__header__item lineup__item--number cell small-1`}>#</span>
266
        <span className={`lineup__header__item lineup__item--name cell small-9`}>Name</span>
267
        <span className={`lineup__header__item lineup__item--time cell small-1`}>
268
          <Icon icon="fa-clock-o" />
269
        </span>
270
      </div>
271
    )
272
  }
273
274
  renderSubLine(i, element) {
275
    return (
276
      <div className={`lineup__row lineup__row--substitute grid-x grid-margin-x`} key={i}>
277
        <span
278
          className={`lineup__row__item lineup__item--status cell small-1`}
279
          style={{
280
            backgroundImage: `url(${element.changed ? iconSubIn : iconBench})`,
281
          }}
282
          title={`${element.changed ? `Wisselspeler ingevallen` : `Wisselspeler`}`}
283
        ></span>
284
        <span className={`lineup__row__item lineup__item--number cell small-1`}>{element.number}</span>
285
        <span className={`lineup__row__item lineup__item--name cell small-9`}>{element.playerName}</span>
286
        <span className={`lineup__row__item lineup__item--time cell small-1`}>{element.minutesPlayed}'</span>
287
      </div>
288
    )
289
  }
290
291
  renderLineupLine(i, element) {
292
    return (
293
      <div className={`lineup__row lineup__row--lineup grid-x grid-margin-x`} key={i}>
294
        <span
295
          className={`lineup__row__item lineup__item--status cell small-1`}
296
          style={{
297
            backgroundImage: `url(${element.changed ? iconSubOut : iconStart})`,
298
          }}
299
          title={`${element.changed ? `Basisspeler gewisseld` : `Basisspeler`}`}
300
        ></span>
301
        <span className={`lineup__row__item lineup__item--number cell small-1`}>{element.number}</span>
302
        <span className={`lineup__row__item lineup__item--name cell small-9`}>
303
          {element.playerName} {element.captain && `(C)`}
304
        </span>
305
        <span className={`lineup__row__item lineup__item--time cell small-1`}>{element.minutesPlayed}'</span>
306
      </div>
307
    )
308
  }
309
}
310
311
export const pageQuery = graphql`
312
  query {
313
    site {
314
      siteMetadata {
315
        kcvvPsdApi
316
      }
317
    }
318
  }
319
`
320
321
export default GamePage
322